package com.heeexy.example.controller;

import com.alibaba.fastjson.JSONObject;;
import com.heeexy.example.service.ProjectService;
import com.heeexy.example.util.CommonUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    /**
     * 查询所有项目列表
     */
    @RequiresPermissions("project:list")
    @GetMapping("/listProjectAll")
    public JSONObject listProjectAll(HttpServletRequest request){
        return projectService.listProjectAll(CommonUtil.request2Json(request));
    }

    /**
     * 查询审批中项目列表
     */
    @RequiresPermissions("project:list")
    @GetMapping("/listProject")
    public JSONObject listProject(HttpServletRequest request){
        return projectService.listProject(CommonUtil.request2Json(request));
    }

    /**
     * 新增项目
     */
    @RequiresPermissions("project:add")
    @PostMapping("/addProject")
    public JSONObject addProject(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson,"proId, userId, proName, proCost, proResource, proContent");
        return projectService.addProject(requestJson);
    }

    /**
     * 修改项目
     */
    @RequiresPermissions(value = {"project:add","project:update"},logical = Logical.OR)
    @PostMapping("/updateProject")
    public JSONObject updateDevice(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "proId, proApprove");
        return projectService.updateProject(requestJson);
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("project:delete")
    @PostMapping("/deleteProject")
    public JSONObject deleteDevice(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "proId, deleteStatus");
        return projectService.deleteProject(requestJson);
    }

    /**
     * 用户申请项目
     */
    @PostMapping("/submitProject")
    public JSONObject submitProject(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson,"name,cost,resourse,desc,remark");
        return projectService.submitProject(requestJson);
    }

    /**
     *读取当前用户的项目
     */
    @GetMapping("/userProjectList")
    public JSONObject userProjectList(HttpServletRequest request) {
        return projectService.preUserProjectList(CommonUtil.request2Json(request));
    }

    /**
     * 暂停 开始项目 改变SQL的stateGoing字段
     */
    @PostMapping("/updateStateGoing")
    public JSONObject updateStateGoing(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson,"stateGoing,proId");
        return projectService.updateProjectState(requestJson);
    }

    /**
     * 用户更新申请项目的信息
     */
    @PostMapping("/updateApplyProject")
    public JSONObject updateApplyProject(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson,"proName,proContent,proCost,proResource,proId");
        return projectService.updateApplyProject(requestJson);
    }

    /**
     * 用户完成项目
     */
    @PostMapping("/updateProjectState")
    public JSONObject updateProjectState(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson,"proState,proId");
        return projectService.updateProjectComplete(requestJson);
    }
}
